-- hp_to_defense
-- created by zhuangsl
-- 每损失20%的生命值时攻击百分比提升

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    trigger = function(who, prop, defense)
        local rate = math.modf(1000 * who:queryAttrib("hp") / who:queryAttrib("max_hp"));
        if rate >= 1000 then
            return defense;
        end
        
        rate = math.max(0, rate);
        local value = tonumber(prop[2])
        local num = math.floor((1000 - rate) / value);

        local defense = defense + num * prop[3];

        return defense;
    end,

    record = function(target, prop, path)
        return false;
    end
};